import request from '@/utils/request'

export const addAvatarService = (avatar) => {
  const fd = new FormData()
  fd.append('file', avatar)
  return request.post('/avatar/', fd)
}

export const imageUrlToFileObject = async (imageUrl) => {
  try {
    console.log(imageUrl)
    // 使用 Axios 下载图片数据
    const response = await request.get(imageUrl, {
      responseType: 'arraybuffer'
    })

    console.log(response.data)

    const blob = new Blob([response.data], {
      type: response.headers['content-type']
    })

    // 创建 File 对象
    const file = new File([blob], imageUrl, {
      type: response.headers['content-type']
    })
    // 将下载的数据转换成 Blob 对象
    /*const blob = new Blob([response.data])

    // 创建 File 对象
    const file = new File([blob], imageUrl, {
      type: getFileType(imageUrl)
    })*/
    console.log(file)
    console.log(URL.createObjectURL(file))
    return URL.createObjectURL(file)
  } catch (error) {
    console.error('Error converting image URL to File object:', error)
    return null
  }
}
